package com.cheyun.netsalev3.widget

import android.app.DatePickerDialog
import android.app.Dialog
import android.os.Bundle
import android.widget.DatePicker
import androidx.fragment.app.DialogFragment
import java.util.*
import kotlin.reflect.KFunction4

class DatePickerFragment : DialogFragment(), DatePickerDialog.OnDateSetListener {
    //回调函数
    var clickOk:
            KFunction4<@ParameterName(name = "view") DatePicker,
                    @ParameterName(name = "year") Int, @ParameterName(name = "month") Int,
                    @ParameterName(name = "day") Int, Unit>? = null

    var minDate: Long = 0L
    var maxDate: Long = 0L
    var isTHEME_HOLO_LIGHT:Boolean = false
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        //使用当前日期作为选取器中的默认日期
        val c = Calendar.getInstance()
        val year = c.get(Calendar.YEAR)
        val month = c.get(Calendar.MONTH)
        val day = c.get(Calendar.DAY_OF_MONTH)
        //创建一个新的datepickerDialog实例并返回它
        var datePickerDialog:DatePickerDialog? = null;
        if(isTHEME_HOLO_LIGHT){
            datePickerDialog = DatePickerDialog(activity, DatePickerDialog.THEME_HOLO_LIGHT,this, year, month, day)
        }else{
            datePickerDialog = DatePickerDialog(activity, this, year, month, day)
        }
//        var datePickerDialog = DatePickerDialog(activity, this, year, month, day)
//        var datePickerDialog = DatePickerDialog(activity, DatePickerDialog.THEME_HOLO_LIGHT,this, year, month, day)

        var datePicker = datePickerDialog.datePicker
        if (this.minDate != 0L) {
            datePicker.minDate = minDate
        }
        if (this.maxDate != 0L) {
            datePicker.maxDate = maxDate
        }
        return datePickerDialog
    }

    override fun onDateSet(view: DatePicker, year: Int, month: Int, day: Int) {
        //对用户选择的日期进行操作
        clickOk?.apply {
            this(view, year, month+1, day)
        }
    }
}